<?php

class Cobatimeline extends CI_Controller {

    var $_public_view;

    function __construct() {
        parent::__construct();
        $this->load->model('model_perjalanan', 'mtrip');
        $this->load->helper('url');
        $this->load->library('session');
        $this->config->load('ojexcom');
        $this->_public_view = $this->config->item('public_view');
    }

    function index() {

        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        $data = array(
            'news' => $this->tes(),
            'user'=> $user, 'id' => $id,
            'isLogin' => $isLogin,
            'page_title' => 'Coba timeline',
            'page_content' => 'cobatimeline');
       $this->load->view($this->_public_view, $data);
       //$this->load->view('cari');
     }

     function tes(){
        if(isset($_POST['addnews'])){
            $news = filter_input(INPUT_POST, 'news', FILTER_SANITIZE_SPECIAL_CHARS);
            $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
            // $sql = "INSERT INTO news (description, name, date) VALUES ('".$news."', '".$name."', '".date('Y-m-d H:i:s')."')";
            // mysql_query($sql);
            $data_input = array('name'=>$name,'description'=>$news);
            $this->db->input('news',$data_input);
        }
        /**
        * Preparing and getting response for latest feed items.
        **/
        if(isset($_POST['latest_news_time'])){  
            $sql = "SELECT * FROM news WHERE date > '".$this->input->post('latest_news_time')."' ORDER BY date DESC";
            $resource = $this->db->query($sql);
            //$resource = mysql_query($sql);
            $current_time = $this->input->post('latest_news_time');
            //$item = mysql_fetch_assoc($resource);
            $item = $resource->row();
            $last_news_time = $item['date'];
            while ($last_news_time < $current_time) {
                usleep(1000); //giving some rest to CPU
                $resource = $this->db->query($sql);
                //$item = mysql_fetch_assoc($resource);
                $item = $resource->row();
                $last_news_time = $item['date'];
            }
             ?>
            <li id="<?php echo $item['date'] ?>">
                 <span class="feedtext"><?php echo $item['description'] ?> was added by <b><?php echo $item['name'] ?></b></span>
            </li>
             <?php
            exit;
        }

        /**
        * Getting news Items and preparing sql query with respect to request
        **/
        $sql = "SELECT * FROM news ORDER BY date DESC LIMIT 0, 10";
        if(isset($_POST['last_time'])){
            $sql = "SELECT * FROM news WHERE date < '".$_POST['last_time']."' ORDER BY date DESC LIMIT 0, 10";
        }
        $resource = $this->db->query($sql);
        $news = array();
        $counter = 0;
        foreach($resource->result() as $row){
            $news[$counter]['name'] = $row->name;
            $news[$counter]['description'] = $row->description;
            $news[$counter]['date'] = $row->description;
            $counter++;
        }
        return $news;
    }

    function masuk(){
        $today = date("Y-m-d H:i:s");
        $input = array(
            'name'=>$this->input->post('name'),
            'description' => $this->input->post('news'),
            'date' => $today
            );
        $this->db->insert('news',$input);
    }

    function get(){
        $sql = "SELECT * FROM news ORDER BY date DESC LIMIT 0, 10";
        if(isset($_POST['last_time'])){
            $sql = "SELECT * FROM news WHERE date < '".$_POST['last_time']."' ORDER BY date DESC LIMIT 0, 10";
        }
        $resource = $this->db->query($sql);
        $news = array();
        $counter = 0;
        foreach($resource->result() as $row){
            $news[$counter]['name'] = $row->name;
            $news[$counter]['description'] = $row->description;
            $news[$counter]['date'] = $row->description;
            $counter++;
        }
        return $news;
    }

}

?>
